#include "re.h"

bool re_matched(const char *src, const char *pattern) {
    pcre *re;
    const char *error;
    int erroffset;
    int rc;

    re = pcre_compile(pattern,    // the pattern
                      0,          // default options
                      &error,     // for error message
                      &erroffset, // for error offset
                      NULL);      // use default character tables

    // compilation failed
    if (re == NULL) {
        return 0;
    }

    rc = pcre_exec(re,          // the compiled pattern
                   NULL,        // no extra data
                   src,         // the src string
                   strlen(src), // the length of the src
                   0,           // start at offset 0 in the src
                   0,           // default options
                   NULL, 0);
    free(re); // finished matching

    // not matched
    if (rc < 0) {
        return false;
    }

    return true;
}
